package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.domain.Jobs;
import com.springboot.hiresystem.dto.req.JobsQueryRequestDTO;
import com.springboot.hiresystem.dto.req.JobsUpdateRequest;
import com.springboot.hiresystem.dto.resp.JobsResponseDTO;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.service.JobsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jobs")
@Slf4j
public class JobsController {
    private JobsService jobsService;

    public JobsController(JobsService jobsService) {
        this.jobsService = jobsService;
    }

// 带分页和条件查询
    @GetMapping("/list")
public Result<PageResponse<JobsResponseDTO>> list(JobsQueryRequestDTO jobsQueryRequestDTO) {

    // 查询条件
    QueryWrapper<Jobs> queryWrapper = new QueryWrapper<>();
    queryWrapper.like(jobsQueryRequestDTO.getTitle() != null, "title", jobsQueryRequestDTO.getTitle());

    // 分页信息
    Page<Jobs> page = new Page<>(jobsQueryRequestDTO.getPage(), jobsQueryRequestDTO.getSize());
    // 分页查询
    Page<Jobs> jobsPage = jobsService.page(page, queryWrapper);
    // 获取结果集
    List<Jobs> jobs = jobsPage.getRecords();
    log.info("查询结果:{}", jobs);

    // 日志输出总记录数和总页数
    log.info("总记录数:{},总页数:{}", jobsPage.getTotal(), jobsPage.getPages());

    PageResponse<JobsResponseDTO> pageResponse = new PageResponse<>();
    // 转换成DTO
    List<JobsResponseDTO> jobsResponseDTOS = BeanUtil.copyToList(jobs, JobsResponseDTO.class);
    pageResponse.setTotal(jobsPage.getTotal());
    pageResponse.setList(jobsResponseDTOS);
    System.out.println("-----------------"+pageResponse.toString());
    System.out.println("----"+Result.success(pageResponse));
        // 返回结果
    return Result.success(pageResponse);
}

    // 条件查询
    @PostMapping("/query")
    public Result<List<JobsResponseDTO>> query(@RequestBody JobsQueryRequestDTO jobsQueryRequestDTO) {
        QueryWrapper<Jobs> queryWrapper = new QueryWrapper<>();
        if (jobsQueryRequestDTO.getJobId() != null) {
            queryWrapper.eq("job_id", jobsQueryRequestDTO.getJobId());
        }
        if (jobsQueryRequestDTO.getTitle()!= null) {
            queryWrapper.like("title", jobsQueryRequestDTO.getTitle());
        }
        List<Jobs> jobs = jobsService.list(queryWrapper);
        if (jobs.isEmpty()){
            return Result.failure(500,"未查询到");
        }else {
        List<JobsResponseDTO> jobsResponseDTOS = BeanUtil.copyToList(jobs, JobsResponseDTO.class); // 使用 BeanUtil.copyToList
        return Result.success(jobsResponseDTOS); // 自动设置 count
        }
    }
    //更新
    @PostMapping("/update")
    public Result<String> update(@RequestBody JobsUpdateRequest jobsUpdateRequest) {
        Jobs jobs = BeanUtil.copyProperties(jobsUpdateRequest, Jobs.class);
        System.out.println("--------------update"+jobs.toString());
        if(jobsService.updateById(jobs)){
            return Result.success("更新成功");
        }else{
            return Result.failure(500,"更新失败");
        }
    }
    //添加
    @PostMapping("/add")
    public Result<String> add(@RequestBody Jobs jobs) throws Exception {
        System.out.println("--------------add"+jobs.toString());
        if (!jobsService.save(jobs)){
            return Result.failure(500,"添加失败");
        }
        return Result.success("添加成功");
    }
    //按照id删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Jobs jobs) {
        if (jobsService.removeById(jobs.getJobId())){
            return Result.success("删除成功");
        }else {
            return Result.failure(500,"删除失败");
        }
    }
}
